package com.lam.mall.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lam.common.base.controller.BaseController;
import com.lam.common.base.domain.Result;
import com.lam.common.base.domain.tree.TreeNode;
import com.lam.common.base.domain.tree.TreeUtil;
import com.lam.common.base.page.TableData;
import com.lam.common.components.dict.annotation.DictAutoTransfer;
import com.lam.common.constant.CommonConstant;
import com.lam.common.utils.JsonUtil;
import com.lam.common.utils.StringUtils;
import com.lam.common.validate.ValidateUtil;
import com.lam.common.validate.group.Add;
import com.lam.common.validate.group.Update;
import com.lam.common.validate.model.ValidOperType;
import com.lam.framework.annotation.ParamInject;
import com.lam.framework.operLog.annotation.Log;
import com.lam.framework.operLog.enums.BusinessType;
import com.lam.mall.common.constant.MallConstant;
import com.lam.mall.entity.Area;
import com.lam.mall.param.AreaParam;
import com.lam.mall.service.AreaService;

/**
 * 地区管理Controller
 * 
 * @author admin
 * @date 2023-04-20
 */
@Log(title = "地区管理")
@ParamInject
@RestController
@RequestMapping("/${adminPath}/mall/area")
public class AreaController extends BaseController {

    @Autowired
    private AreaService areaService;

    /**
     * 查询地区管理列表
     */
    @Log(oper="分页查询")
    @PreAuthorize("@ss.hasPm('mall:area:list')")
    @GetMapping("/list")
    public Result list(AreaParam param) {
    
    	List<Area> list = areaService.selectList(param);
        list = TreeUtil.buildTree(list);
        return Result.ok(new TableData(list, list.size()));
    }
    
    /**
     * 地区管理下拉树列表
     */
    @Log(oper="地区管理下拉树列表")
    @PreAuthorize("@ss.hasPm('mall:area:list')")
    @GetMapping("/treeselect")
    public Result treeselect(AreaParam param) {
    
    	//PageHelper.orderBy("parent_id, sort_no");
        List<Area> list = areaService.selectList(param);
        List<TreeNode> treeNodeList = JsonUtil.toJavaList(list, TreeNode.class, "id", "parentId", "areaName=>title", "sortNo");
        return Result.ok(TreeUtil.buildTree(treeNodeList, CommonConstant.TOP_PARENT_ID));
    }


//    /**
//     * 通过父级id获取区域列表
//     */
//    @Log(oper="查询区域的子区域")
//    @PreAuthorize("@ss.hasPm('mall:area:subList')")
//    @GetMapping("/listByPid")
//    public Result listByPid(String pid) {
//        List<Area> list = areaService.listByPid(pid);
//        return Result.ok(list);
//    }
    
    /**
     * 获取地区管理详细信息
     */
    @DictAutoTransfer
    @Log(oper="根据ID查询")
    @PreAuthorize("@ss.hasPm('mall:area:query')")
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable("id") String id) {
    
        return Result.ok(areaService.selectById(id));
    }

    /**
     * 新增地区管理
     */
    @PreAuthorize("@ss.hasPm('mall:area:add')")
    @Log(businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@Validated(Add.class) @RequestBody Area area) {
    
    	if(StringUtils.isBlank(area.getParentId())) {
    		area.setParentId("0");
    	}
        ValidateUtil.validUnique(area, ValidOperType.ADD, MallConstant.UNIQUE_AREA_NAME, "名称已存在！");
        int n = areaService.insert(area);
        if(n > 0) {
        	areaService.removeCacheByPId(area.getParentId());
        }
        return toAjax(n);
    }

    /**
     * 修改地区管理
     */
    @PreAuthorize("@ss.hasPm('mall:area:edit')")
    @Log(businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@Validated(Update.class) @RequestBody Area area) {
    	if(StringUtils.isBlank(area.getParentId())) {
    		area.setParentId("0");
    	}
        ValidateUtil.validUnique(area, ValidOperType.UPDATE, MallConstant.UNIQUE_AREA_NAME, "名称已存在！");
        int n = areaService.update(area);
        if(n > 0) {
        	areaService.removeCacheByPId(area.getParentId());
        }
        return toAjax(n);
    }

    /**
     * 删除地区管理
     */
    @PreAuthorize("@ss.hasPm('mall:area:remove')")
    @Log(businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public Result remove(@PathVariable String id) {
    
    	Area area = areaService.selectById(id);
    	int n = areaService.deleteById(id);
        if(n > 0) {
        	areaService.removeCacheByPId(area.getParentId());
        }
        return toAjax(n);
    }

}
